package com.vallis.asfc.controller;


import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vallis.asfc.entity.Organization;
import com.vallis.asfc.entity.User;
import com.vallis.asfc.service.OrganizationService;
import com.vallis.asfc.util.NativePlace;
import com.vallis.asfc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vallis
 * @since 2020-06-14
 */
@RestController
@CrossOrigin
@RequestMapping("/organization")
public class OrganizationController {
    @Autowired
    private OrganizationService organizationService;
    @GetMapping("getOrgList")
    public Result getOrgList(){
        List<Organization> orgList = organizationService.list(new QueryWrapper<Organization>().ne("org_name", "平台"));
        System.out.println(orgList.size());
        return new Result(200,"获取成功",orgList);
    }
    
    /**
     * 分页查询
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getOrganizations")
    public Result getOrganizations(String query,Integer pageNum,Integer pageSize){
        System.out.println("调用分页查询");
        System.out.println("获取的名字是："+query);
        QueryWrapper<Organization> wrapper = new QueryWrapper();
        if (null!=query){
            QueryWrapper<Organization> userName = wrapper.like("org_name", query);
            Page<Organization> page=new Page<Organization>(pageNum,pageSize);
            
            Page<Organization> page1 = organizationService.page(page, userName);
            
            return  new Result(200,"调用成功",page1);
        }else {
            wrapper.gt("user_id",0);
            Page<Organization> page=new Page<Organization>(pageNum,pageSize);
            
            Page<Organization> page1 = organizationService.page(page,wrapper);
            
            return  new Result(200,"调用成功",page1);
            
        }
        
    }
    /**
     *更改状态
     * @param userId
     * @param state
     * @return
     */
    @PutMapping("updateOrgState")
    public Result updateOrgState(Long userId,int state){
        System.out.println(userId);
        Organization u= organizationService.getById(userId);
        u.setOrgState((long)state);
        return new Result("更新成功",organizationService.updateById(u));
    }
    
    
    /**
     * 根据id查询
     * @param userId
     * @return
     */
    @GetMapping("getById")
    public Result getById(Long userId){
        System.out.println("获取单个用户的id:"+userId);
        return  new Result(organizationService.getById(userId));
    }
    
    
    
    /**
     * 保存用户
     * @param orgName
     * @param orgAddress
     * @return
     */
    @PutMapping("saveOrg")
    public Result saveOrg(String orgName,String orgAddress,int type,String city){
        
      Organization o=new Organization();
        
      o.setOrgName(orgName);
      o.setOrgType(type);
      o.setOrgCity(city);
      o.setOrgAddress(orgAddress);
        return new Result("保存成功",organizationService.save(o));
    }
    
    /**
     * 更新用户
     *
     * @param user
     *
     * @return
     */
    @PostMapping("updateOrganization")
    public Result updateOrganization(Organization user) {
        return new Result("更新成功", organizationService.updateById(user));
    }
    
    
    /**
     * 获取正式会员单位的数量
     *
     * @return
     */
    @GetMapping("getFormalUserNum")
    public Result getFormalUserNum() {
        return new Result("单位数量", organizationService.count(new QueryWrapper<Organization>().eq("org_state", 1)));
    }
    
    
    /**
     * 获取会员数量
     *
     * @return
     */
    @GetMapping("getUserNum")
    public Result getUserNum() {
        
        return new Result("会员单位数量", organizationService.count());
    }
    
    /**
     * 获取非正式会员单位的数量
     *
     * @return
     */
    @GetMapping("getUnFormalUserNum")
    public Result getUnFormalUserNum() {
        int user_state = organizationService.count(new QueryWrapper<Organization>().ne("org_state", 1));
        System.out.println(user_state);
        return new Result("单位数量", user_state);
    }
    
    /**
     * 获取正式会员单位
     *
     * @return
     */
    @GetMapping("showDeptInWelcome")
    public Result showDeptInWelcome() {
        System.out.println("在欢迎页获取已交费单位");
        return new Result("单位数量", organizationService.list(new QueryWrapper<Organization>().eq("org_state", 1)));
    }
}
